package com.day13_Internet;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @ClassName: UDP_receive_demo
 * @Author: zw
 * @Description: 出百万拳，行万里路；
 * @Create 2024/3/24-13:56
 * @Version 1.0
 */
public class UDP_receive_demo {
    public static void main(String[] args) throws IOException {
        // 1.
        DatagramSocket receive = new DatagramSocket(4444);
        // 2
        byte[] bys = new byte[1024];
        DatagramPacket dp =new DatagramPacket(bys, bys.length);
        //3.
        receive.receive(dp);
        // 4.解析数据包，然后在控制台显示
        byte[] data = dp.getData();
        // dp.getLength 实际接受的长度
        String dataString = new String(data, 0, dp.getLength()); // 将字节数据组数据变成字符串
        System.out.println("接受方："+dataString);
        // 5.
        receive.close();
    }
}
